package com.devops.admin.graphql;

import com.devops.admin.model.DOSResource;
import com.devops.admin.model.TableResult;
import com.devops.admin.po.DOSBindResourcePo;
import com.devops.admin.po.DOSResourcePo;
import com.devops.admin.service.DOSResourceProjectRelationService;
import com.devops.admin.service.DOSResourceService;
import com.devops.admin.vo.DOSPageVo;
import com.devops.admin.vo.DOSResourceVo;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName DOSResourceDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/4 11:12
 * @Version 1.0
 **/
@DgsComponent
public class DOSResourceDataFetcher {

    @Autowired
    private DOSResourceService dosResourceService;
    @Autowired
    private DOSResourceProjectRelationService dosResourceProjectRelationService;

    /**
     * @Author heyabo
     * @Description 添加资源信息
     * @Date 14:15 2022/6/8
     * @Param [resource]
     * @return com.devops.admin.model.DOSResource
     **/
    @DgsMutation
    @DOSLog(description = "添加资源信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public Boolean saveResource(@InputArgument DOSResource resource){

        return dosResourceService.saveResource(resource);
    }

    /**
     * @Author heyabo
     * @Description 更新资源信息
     * @Date 14:25 2022/6/8
     * @Param [resource]
     * @return com.devops.admin.model.DOSResource
     **/
    @DgsMutation
    @DOSLog(description = "更新资源信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public Boolean updateResource(@InputArgument DOSResource resource){

        return dosResourceService.updateResource(resource);
    }

    /**
     * @Author heyabo
     * @Description 删除资源信息
     * @Date 15:21 2022/6/8
     * @Param [resourceId]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "删除资源信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public Boolean deleteResource(@InputArgument Integer resourceId){

        return dosResourceService.removeResource(resourceId);
    }

    /**
     * @Author heyabo
     * @Description 绑定资源和项目
     * @Date 08:56 2022/6/9
     * @Param [resourceId, projectId]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "绑定资源和项目")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public boolean bindResourceProject(@InputArgument Integer resourceId, @InputArgument List<Integer> projectIds){

        return dosResourceProjectRelationService.bindRelation(resourceId,projectIds);
    }

    /**
     * @Author heyabo
     * @Description 普通用户绑定项目和资源
     * @Date 13:55 2022/6/13
     * @Param [resourceId, projectId]
     * @return boolean
     **/
    @DgsMutation
    @DOSLog(description = "普通用户绑定项目和资源")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public boolean ordinaryBindResourceProject(@InputArgument List<Integer> resourceIds, @InputArgument Integer projectId, Integer type){

        return dosResourceProjectRelationService.bindResourceProject(resourceIds,projectId,type);
    }

    /**
     * @Author heyabo
     * @Description 解绑资源和项目
     * @Date 09:51 2022/6/9
     * @Param [resourceId, projectId]
     * @return boolean
     **/
    @DgsMutation
    @DOSLog(description = "解绑资源和项目")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public boolean noBindResourceProject(@InputArgument Integer resourceId, @InputArgument Integer projectId){

        return dosResourceProjectRelationService.noBindRelation(resourceId,projectId);
    }

    /**
     * @Author heyabo
     * @Description 分页查询资源信息
     * @Date 15:47 2022/6/8
     * @Param [resource, pageVo]
     * @return com.devops.admin.model.TableResult<com.devops.admin.model.DOSResource>
     **/
    @DgsQuery
    @DOSLog(description = "分页查询资源信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public TableResult<DOSResourcePo> getResources(@InputArgument DOSResourceVo resource, @InputArgument DOSPageVo pageVo){

        return dosResourceService.queryResources(resource,pageVo);
    }

    /**
     * @Author heyabo
     * @Description 查询绑定和未绑定项目的资源
     * @Date 16:53 2022/6/13
     * @Param [projectId, type]
     * @return java.util.List<com.devops.admin.po.DOSBindResourcePo>
     **/
    @DgsQuery
    @DOSLog(description = "查询绑定和未绑定项目的资源")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public List<DOSBindResourcePo> getBindResource(@InputArgument Integer projectId, @InputArgument Integer type){

        return dosResourceService.getBindResource(projectId,type);
    }

    /**
     * @Author heyabo
     * @Description 获取redis列表
     * @Date 15:11 2022/9/22
     * @Param []
     * @return java.util.List<com.devops.admin.model.DOSResource>
     **/
    @DgsQuery
    public List<DOSResource> getRedisList(){

        return dosResourceService.getRedisList();
    }

    @DgsQuery
    public List<DOSResource> getDatabases(Integer type){

        return dosResourceService.getDatabases(type);
    }
}
